-module(ring).
-export([start/2]).

start(N, M) ->
    Next = create(N-1, M, self()),
    statistics(wall_clock),
    Next ! M,
    loop(Next, M),
    {_, T} = statistics(wall_clock),
    io:format("~p processes run ~p rings used: ~pms.~n", [N, M, T]).

create(1, _, P) -> P;
create(N, M, P) ->
    spawn(fun() -> Next = create(N-1, M, P), loop(Next, M) end).

loop(_, 0) -> void;
loop(Next, M) ->
    receive
        Message ->
            Next ! Message,
            loop(Next, M-1)
    end.
